package com.yh.zz_hlj.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.yh.zz_hlj.AppContext;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kangdong on 2016/2/25.
 */
public class WorkDatabase {
    private final DatabaseHelper dbHelper;

    public WorkDatabase(Context context) {
        super();
        dbHelper = new DatabaseHelper(context);
    }

    /**
     * 增
     *
     * @param  userId,String username,String hbWork
     */
    public void insert(int userId,String username,String hbWork) {
        String sql = "insert into " + DatabaseHelper.Work_TABLE_NAME;

        sql += "(FHBUser, FHBUserName, FBillNo)"
                + " values(?,?,?)";
        try {
            SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
            sqlite.execSQL(sql, new String[]{
                    userId + "", username + "", hbWork});
            sqlite.close();
        } catch (Exception ex) {

        }
    }

    /**
     * 查
     *
     * @param
     * @return
     */
    public  List<String> query() {
        SQLiteDatabase sqlite = dbHelper.getReadableDatabase();
        Cursor cursor = sqlite.rawQuery("select * from "
                + DatabaseHelper.Work_TABLE_NAME, null);
        List< String> list = new ArrayList<String>();
        int user=AppContext.getInstance().getLoginUid();
        if (user!=0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int uid=cursor.getInt(1);
                if (user==uid) {
                    list.add(cursor.getString(3));
                }
            }
            if (!cursor.isClosed()) {
                cursor.close();
            }
            sqlite.close();
        }
        return list;
    }

    /**
     * 删
     *
     * @param
     */
    public void delete() {
        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        String sql ="delete from "+DatabaseHelper.Work_TABLE_NAME;
        sqlite.execSQL(sql);
        sqlite.close();
    }
}
